home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Tools / Languages / Harvest C 1.3 / Application / Examples / Harvest MiniEdit / mini.windows.c < prev    next >
Encoding:
Text File  |  1992-03-17  |  5.1 KB  |  232 lines  |  [TEXT/KAHL]

  1. n Str255          theFileName;
  2. extern int             linesInFolder;
  3.  
  4.     /**
  5.      **        Prototypes for private functions.
  6.      **        (They really should be static.)
  7.      **
  8.      **/
  9.  
  10. int AdjustText (void);
  11. int SetVScroll(void);
  12. int SetView (WindowPtr w);
  13. pascal void ScrollProc (ControlHandle theControl, int theCode);
  14.  
  15.  
  16.  
  17. int SetUpWindows(void)
  18. {
  19.     Rect    destRect, viewRect;
  20.     Rect    vScrollRect;
  21.     FontInfo    myInfo;
  22.     int        height;
  23.     
  24.     SetPort((myWindow = GetNewWindow(windowID, &wRecord, (WindowPtr) -1L)));
  25.     TextFont(monaco);
  26.     TextSize(9);
  27.     vScrollRect = (*myWindow).portRect;
  28.     vScrollRect.left = vScrollRect.right-15;
  29.     vScrollRect.right += 1;
  30.     vScrollRect.bottom -= 14;
  31.     vScrollRect.top -= 1;
  32.     vScroll = NewControl( myWindow, &vScrollRect, "\p", 1, 0, 0, 0,
  33.         scrollBarProc, 0L);
  34.  
  35.     viewRect = qd.thePort->portRect;
  36.     viewRect.right -= SBarWidth;
  37.     viewRect.bottom -= SBarWidth;
  38.     InsetRect(&viewRect, 4, 4);
  39.     TEH = TENew(&viewRect, &viewRect);
  40.     SetView(qd.thePort);
  41.     dirty = 0;
  42. }
  43.  
  44.  
  45. int AdjustText (void)
  46.  
  47. {
  48.     int        oldScroll, newScroll, delta;
  49.     
  50.     oldScroll = (**TEH).viewRect.top - (**TEH).destRect.top;
  51.     newScroll = GetCtlValue(vScroll) * (**TEH).lineHeight;
  52.     delta = oldScroll - newScroll;
  53.     if (delta != 0)
  54.       TEScroll(0, delta, TEH);
  55. }
  56.  
  57.  
  58. int SetVScroll(void)
  59. {
  60.     register int    n;
  61.     
  62.     n = (**TEH).nLines-linesInFolder;
  63.  
  64.     if ((**TEH).teLength > 0 && (*((**TEH).hText))[(**TEH).teLength-1]=='\r')
  65.         n++;
  66.  
  67.     SetCtlMax(vScroll, n > 0 ? n : 0);
  68. }
  69.  
  70. int ShowSelect (void)
  71.  
  72. {
  73.     register    int        topLine, bottomLine, theLine;
  74.     
  75.     SetVScroll();
  76.     AdjustText();
  77.     
  78.     topLine = GetCtlValue(vScroll);
  79.     bottomLine = topLine + linesInFolder;
  80.     
  81.     if ((**TEH).selStart < (**TEH).lineStarts[topLine] ||
  82.             (**TEH).selStart >= (**TEH).lineStarts[bottomLine]) {
  83.         for (theLine = 0; (**TEH).selStart >= (**TEH).lineStarts[theLine]; theLine++)
  84.             ;
  85.         SetCtlValue(vScroll, theLine - linesInFolder / 2);
  86.         AdjustText();
  87.     }
  88. }
  89.  
  90. int SetView (WindowPtr w)
  91.  
  92. {
  93.     (**TEH).viewRect = w->portRect;
  94.     (**TEH).viewRect.right -= SBarWidth;
  95.     (**TEH).viewRect.bottom -= SBarWidth;
  96.     InsetRect(&(**TEH).viewRect, 4, 4);
  97.  
  98.     linesInFolder = ((**TEH).viewRect.bottom-(**TEH).viewRect.top)/(**TEH).lineHeight;
  99.     (**TEH).viewRect.bottom = (**TEH).viewRect.top + (**TEH).lineHeight*linesInFolder;
  100.     (**TEH).destRect.right = (**TEH).viewRect.right;
  101.     TECalText(TEH);
  102. }
  103.  
  104. int UpdateWindow (WindowPtr theWindow)
  105.  
  106. {
  107.     GrafPtr    savePort;
  108.     
  109.     GetPort(&savePort);
  110.     SetPort(theWindow);
  111.  
  112.     BeginUpdate(theWindow);
  113.     EraseRect(&theWindow->portRect);
  114.     DrawControls(theWindow);
  115.     DrawGrowIcon(theWindow);
  116.     TEUpdate(&theWindow->portRect, TEH);
  117.     EndUpdate(theWindow);
  118.  
  119.     SetPort(savePort);
  120. }
  121.  
  122.  
  123.  
  124.  
  125. pascal void ScrollProc (ControlHandle theControl, int theCode)
  126.  
  127. {
  128.     int    pageSize;
  129.     int    scrollAmt;
  130.     int oldCtl;
  131.     
  132.     if (theCode == 0)
  133.         return ;
  134.     
  135.     pageSize = ((**TEH).viewRect.bottom-(**TEH).viewRect.top) / 
  136.             (**TEH).lineHeight - 1;
  137.             
  138.     switch (theCode) {
  139.         case inUpButton: 
  140.             scrollAmt = -1;
  141.             break;
  142.         case inDownButton: 
  143.             scrollAmt = 1;
  144.             break;
  145.         case inPageUp: 
  146.             scrollAmt = -pageSize;
  147.             break;
  148.         case inPageDown: 
  149.             scrollAmt = pageSize;
  150.             break;
  151.     }
  152.  
  153.     oldCtl = GetCtlValue(theControl);
  154.     SetCtlValue(theControl, oldCtl+scrollAmt);
  155.  
  156.     AdjustText();
  157.  
  158. }
  159.  
  160. int DoContent(WindowPtr theWindow, EventRecord *theEvent)
  161.  
  162. {
  163.     int                cntlCode;
  164.     ControlHandle     theControl;
  165.     int                pageSize;
  166.     GrafPtr            savePort;
  167.     
  168.     GetPort(&savePort);
  169.     SetPort(theWindow);
  170.  
  171.     GlobalToLocal(&theEvent->where);
  172.     if ((cntlCode = FindControl(theEvent->where, theWindow, &theControl)) == 0) {
  173.         if (PtInRect(theEvent->where, &(**TEH).viewRect))
  174.             TEClick(theEvent->where, (theEvent->modifiers & shiftKey)!=0, TEH);
  175.     } else if (cntlCode == inThumb) {
  176.         TrackControl(theControl, theEvent->where, 0L);
  177.         AdjustText();
  178.     } else
  179.         TrackControl(theControl, theEvent->where, &ScrollProc);
  180.  
  181.     SetPort(savePort);
  182. }
  183.  
  184. int MyGrowWindow(WindowPtr w, Point p)
  185.  
  186. {
  187.     GrafPtr    savePort;
  188.     long    theResult;
  189.     Rect    oldHorizBar;
  190.     Rect     r;
  191.     
  192.     GetPort(&savePort);
  193.     SetPort(w);
  194.     
  195.     oldHorizBar = w->portRect;
  196.     oldHorizBar.top = oldHorizBar.bottom - (SBarWidth+1);
  197.  
  198.     SetRect(&r, 80, 80, qd.screenBits.bounds.right, qd.screenBits.bounds.bottom);
  199.     theResult = GrowWindow(w, p, &r);
  200.     if (theResult == 0)
  201.       return 0;
  202.     SizeWindow( w, LoWord(theResult), HiWord(theResult), false);
  203.  
  204.     InvalRect(&w->portRect);
  205.     
  206.     SetView(w);
  207.  
  208.     EraseRect(&oldHorizBar);
  209.     
  210.     MoveControl(vScroll, w->portRect.right - SBarWidth, w->portRect.top-1);
  211.     SizeControl(vScroll, SBarWidth+1, w->portRect.bottom - w->portRect.top-(SBarWidth-2));
  212.     r = (**vScroll).contrlRect;
  213.     ValidRect(&r);
  214.  
  215.  
  216.     SetVScroll();
  217.     AdjustText();
  218.     
  219.     SetPort(savePort);
  220. }
  221.  
  222.  
  223.  
  224. int CloseMyWindow(void)
  225. {
  226.     HideWindow(myWindow);
  227.     TESetSelect(0, (**TEH).teLength, TEH);
  228.     TEDelete(TEH);
  229.     SetVScroll();
  230.     SetUpFiles();
  231. }
  232.